package com.hotelsystem.reservations.persons;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

import com.google.gson.Gson;

public class IOHelper {
	
	ArrayList <Client> clients = new ArrayList<Client>();
	private int directoryNumber=0;
	
	/**
	 * @param path of file
	 * @return string of all content
	 */
	public static String getFileContent(String path) {

		File directoryFile = new File(path);

		String readedJson = "";
		try (Scanner sc = new Scanner(directoryFile)) {
			while (sc.hasNext()) {
				readedJson += sc.next();
			}
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		}
		return readedJson;
	}
	
	public ArrayList<Client> getClients(String path) {

		File directoryFile = new File(path);
		File[] files = directoryFile.listFiles();

		Gson gson = new Gson();

		for (File file : files) {

			if (file.listFiles()!=null){
				directoryNumber++;
				continue;
			}
			
			try (Scanner sc = new Scanner(file)){

				
				Client cl = gson.fromJson(sc.next(), Client.class);
				clients.add(cl);
				System.out.println(cl.toString());

			} catch (FileNotFoundException e) {
                System.out.println("Error reading file "+ file.getName());
				e.printStackTrace();
			}
			
		} 

		return clients;
	}

	public int getDirectoryNumber() {
		return directoryNumber;       //returns directory found number
	}	
	
}
